﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KeyboardImageViewer
{
    public static class ImageCache
    {
        public const int NumImagesToCacheAhead = 1;

        private static CachedImage[] cachedImages;

        static ImageCache()
        {
            Clear();
        }

        public static CachedImage Current
        {
            get
            {
                return cachedImages[NumImagesToCacheAhead];
            }

            set
            {
                cachedImages[NumImagesToCacheAhead] = value;
            }
        }

        public static CachedImage Get(int index)
        {
            return cachedImages[index + NumImagesToCacheAhead];
        }

        public static void Clear()
        {
            cachedImages = new CachedImage[NumImagesToCacheAhead * 2 + 1];
            for (int i = 0; i < cachedImages.Length; i++)
            {
                cachedImages[i] = new CachedImage();
            }
        }

        public static void ShiftLeft()
        {
            for (int i = 0; i < cachedImages.Length - 1; i++)
            {
                cachedImages[i] = cachedImages[i + 1];
            }

            cachedImages[cachedImages.Length - 1] = new CachedImage();
        }

        public static void ShiftRight()
        {
            for (int i = cachedImages.Length - 2; i >= 0; i--)
            {
                cachedImages[i + 1] = cachedImages[i];
            }

            cachedImages[0] = new CachedImage();
        }

        public static void DeleteCurrent(bool lastInList)
        {
            if (lastInList)
            {
                // If this is the last image in the list, move up images behind us.
                for (int i = NumImagesToCacheAhead; i > 0; i--)
                {
                    cachedImages[i] = cachedImages[i - 1];
                }

                cachedImages[0] = new CachedImage();
            }
            else
            {
                // If this is not the last image in the list, move back images ahead of us.
                for (int i = NumImagesToCacheAhead; i < NumImagesToCacheAhead * 2; i++)
                {
                    cachedImages[i] = cachedImages[i + 1];
                }

                cachedImages[NumImagesToCacheAhead * 2] = new CachedImage();
            }

            // Clear out loading flags, recalculate duplicates for cached images.
            foreach (CachedImage cachedImage in cachedImages)
            {
                cachedImage.CallInProgress = false;

                if (ImageManager.Config.TrackDuplicates && cachedImage.Image != null)
                {
                    cachedImage.Image.PopulateDuplicates(ImageManager.Connection);
                }
            }
        }

        public static bool ImageIsInCache(string imagePath)
        {
            foreach (CachedImage cachedImage in cachedImages)
            {
                if (cachedImage.Image != null)
                {
                    string cachedImageName = Database.GetImageName(ImageManager.Connection, cachedImage.Image.ImageId);
                    if (imagePath.ToLowerInvariant() == cachedImageName.ToLowerInvariant())
                    {
                        return true;
                    }
                }
            }

            return false;
        }
    }
}
